﻿using System;
using System.Runtime.Serialization;
using System.Windows;

namespace Game.Common
{
    [DataContract(Namespace="game")]
    public struct GamePoint
    {
        [DataMember(Name="X")]
        public double _X;
        
        [DataMember(Name="Y")]
        public double _Y;
        
        int _HashCode;
        
        public GamePoint(double x, double y)
        {
            _X = x;
            _Y = y;
            _HashCode = 0;
        }
        
        public GamePoint(Point point)
        {
            _X = point.X;
            _Y = point.Y;
            _HashCode = 0;
        }
        
        public GamePoint(GamePoint point)
        {
            _X = point.X;
            _Y = point.Y;
            _HashCode = 0;
        }
        
        public double X 
        {
            get { return _X; }
        }
        
        public double Y 
        {
            get { return _Y; }
        }
        
        #region Equals and GetHashCode implementation
        public override int GetHashCode()
        {
            if (_HashCode > 0)
            {
                return _HashCode;
            }
            
            int hashCode = 0;
            unchecked {
                hashCode += 1000000007 * _X.GetHashCode();
                hashCode += 1000000009 * _Y.GetHashCode();
            }
            return hashCode;
        }
        
        public override bool Equals(object obj)
        {
            return (obj is GamePoint) && Equals((GamePoint)obj);
        }

        public bool Equals(GamePoint other)
        {
            return (_X == other._X) && (_Y == other._Y);
        }
        
        public static bool operator ==(GamePoint lhs, GamePoint rhs)
        {
            return lhs.Equals(rhs);
        }
        
        public static bool operator !=(GamePoint lhs, GamePoint rhs)
        {
            return !(lhs == rhs);
        }
        #endregion

        public Point ToPoint()
        {
            return new Point((int)_X, (int)_Y);
        }
        
        public override string ToString()
        {
            return "(" + _X + "," + _Y + ")";
        }

    }
}
